package com.beiing.tianshuai.tianshuai.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.helper.PendingIntentHelper;

import java.io.InputStream;

public class EasemobService extends Service {

    private static final String TAG = "EasemobService";
    private String mUserName;
    public static int newFriendNum;

    public EasemobService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
//        EMClient.getInstance().contactManager().setContactListener(this);
//        LogUtils.i("onMessageReceived: =================== 开启联系人监听 ==================", TAG);
//        EMClient.getInstance().chatManager().addMessageListener(this);
//        LogUtils.i("onMessageReceived: =================== 开启联系人监听 ==================", TAG);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        EMClient.getInstance().contactManager().removeContactListener(this);
    }

//    @Override
//    public void onContactInvited(String username, String reason) {
//        //收到好友邀请
//        createContactNotificationMessage(username, reason);
//        newFriendNum ++;
//        SPUtils.put(getApplicationContext(), "newFriendCount", newFriendNum);
//        MessageFragment.updateNewFriendPoint();
//        LogUtils.i("=========================== 收到好友请求 ==============================", TAG);
//    }
//
//    @Override
//    public void onFriendRequestAccepted(String username) {
//        //好友请求被同意
//    }
//
//    @Override
//    public void onFriendRequestDeclined(String username) {
//        //好友请求被拒绝
//    }
//
//    @Override
//    public void onContactDeleted(String username) {
//        //被删除时回调此方法
//    }
//
//
//    @Override
//    public void onContactAdded(String username) {
//        //增加了联系人时回调此方法
//    }

    private void createContactNotificationMessage(String username, String reason) {
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
        mBuilder.setDefaults(Notification.DEFAULT_ALL);
        //得到Resources对象
        Resources r = this.getResources();
        //以数据流的方式读取资源
        InputStream inputStream = r.openRawResource(R.raw.logo);
        BitmapDrawable bmpDraw = new BitmapDrawable(inputStream);
        Bitmap bmp = bmpDraw.getBitmap();
        mBuilder.setAutoCancel(true);
        mBuilder.setLargeIcon(bmp);
        mBuilder.setSmallIcon(R.raw.logo);
        mBuilder.setContentText(reason);
        mBuilder.setContentTitle("收到一条好友请求").setTicker(username);
        mBuilder.setContentIntent(getDefaultIntent(Notification.FLAG_AUTO_CANCEL));
        if (notificationManager != null) {
            notificationManager.notify(100, mBuilder.build());
        }
        bmp.recycle();
    }

    public PendingIntent getDefaultIntent(int flags) {
        return PendingIntentHelper.getNewFriendMessagePendingIntent(getApplicationContext(), flags);
    }

//    public PendingIntent getDefaultIntent(int flags, EMMessage msg) {
//        Intent intent = new Intent(getApplicationContext(), EaseChatActivity.class);
//        mUserName = msg.getFrom();
//        if (msg.getChatType() == EMMessage.ChatType.Chat) {
//            intent.putExtra("userId", mUserName);
//            intent.putExtra("chatType", EaseConstant.CHATTYPE_SINGLE);
//            intent.putExtra("userNickname", SPUtils.getString(getApplicationContext(), "nickname" + mUserName, mUserName));
//            intent.putExtra("toAvatar", UserInfoBean.getOtherAvatar(getApplicationContext(), mUserName));
//        } else if (msg.getChatType() == EMMessage.ChatType.GroupChat){
//            intent.putExtra("userId", msg.getTo());
//            intent.putExtra("chatType", EaseConstant.CHATTYPE_GROUP);
//            intent.putExtra("userNickname", SPUtils.getString(getApplicationContext(), "nickname" + mUserName, mUserName));
//            intent.putExtra("toAvatar", UserInfoBean.getOtherAvatar(getApplicationContext(), mUserName));
//        }
//        return PendingIntent.getActivity(this, 1, intent, flags);
//    }

//    @Override
//    public void onMessageReceived(List<EMMessage> messages) {
//        // 收到新消息
//        for (EMMessage message : messages) {
//            try {
//                String avatar = message.getStringAttribute("avatar");
//                String nickname = message.getStringAttribute("nickname");
//                EaseSPUtils.put(getApplicationContext(), "avatar" + message.getFrom(), avatar);
//                EaseSPUtils.put(getApplicationContext(), "nickname" + message.getFrom(), nickname);
//                Log.i(TAG, "onMessageReceived: " + nickname + "/" + avatar);
//            } catch (HyphenateException e) {
//                e.printStackTrace();
//            }
//        }
//        createNewMessageNotification(messages.get(messages.size() - 1));
//        updateChatList();
//        MainActivity.updatePointUI();
//        LogUtils.i("=========================== 收到新消息 ==============================", TAG);
//    }

//    private void createNewMessageNotification(EMMessage emMessage) {
//        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
//        mBuilder.setDefaults(Notification.DEFAULT_ALL);
//        //得到Resources对象
//        Resources r = this.getResources();
//        //以数据流的方式读取资源
//        InputStream inputStream = r.openRawResource(R.raw.logo);
//        BitmapDrawable bmpDraw = new BitmapDrawable(inputStream);
//        Bitmap bmp = bmpDraw.getBitmap();
//        mBuilder.setAutoCancel(true);
//        mBuilder.setLargeIcon(bmp);
//        mBuilder.setSmallIcon(R.raw.logo);
//        switch (emMessage.getType()) {
//            case TXT:
//                String message = ((EMTextMessageBody) emMessage.getBody()).getMessage();
//                if (!message.startsWith("[#Notice]")) {
//                    Spannable span = EaseSmileUtils.getSmiledText(getApplicationContext(), message);
//                    ((ContactListAdapter.ViewHolder) holder).mMessage.setText(span, TextView.BufferType.SPANNABLE);
//                    mBuilder.setContentText(message);
//                } else {
//                    mBuilder.setContentText(message.replace("[#Notice]", ""));
//                }
//                break;
//            case IMAGE:
//                mBuilder.setContentText("[图片]");
//                break;
//            case VIDEO:
//                mBuilder.setContentText("[视频]");
//                break;
//            case LOCATION:
//                mBuilder.setContentText("[地点]");
//                break;
//            case VOICE:
//                mBuilder.setContentText("[语音]");
//                break;
//            case FILE:
//                mBuilder.setContentText("[文件]");
//                break;
//            case CMD:
//                break;
//        }
//        mBuilder.setContentTitle("收到一条新消息").setTicker("收到一条新消息");
//        mBuilder.setContentIntent(getDefaultIntent(PendingIntent.FLAG_UPDATE_CURRENT, emMessage));
//        if (notificationManager != null) {
//            notificationManager.notify(100, mBuilder.build());
//        }
//        bmp.recycle();
//    }

//    @Override
//    public void onCmdMessageReceived(List<EMMessage> messages) {
//
//    }
//
//    @Override
//    public void onMessageRead(List<EMMessage> messages) {
//
//    }
//
//    @Override
//    public void onMessageDelivered(List<EMMessage> messages) {
//
//    }
//
//    @Override
//    public void onMessageRecalled(List<EMMessage> list) {
//
//    }
//
//    @Override
//    public void onMessageChanged(EMMessage message, Object change) {
//
//    }
}
